<template>
    <a-form-model ref="form"  :rules="rules" :model="form" layout="vertical" class="rx-a-form-vertical">
        <a-form-model-item :label="i18n('form.listSelect','列表选择')" prop="boList" >
            <rx-input-button width="80%"  @click="selectBoList" v-model="form.boList" ></rx-input-button>
        </a-form-model-item>
        <a-form-model-item :label="i18n('form.dataPermission','数据权限')" >
            <a-select style="width: 90%;" v-model="form.pmtAlias" :options="dataAuthAry"
                      :getPopupContainer="p=>p.parentNode" allowClear />
        </a-form-model-item>
        <a-form-model-item :label="i18n('form.colMap','列映射')">

            <a-table
                :columns="columns"
                :dataSource="form.fieldMap"
                :pagination="false"
                :bordered="true"
                style="width: 91.5%;"
            >
                <template slot="destField" slot-scope="text, record, index"  >
                    {{record.label}}
                </template>

                <template slot="srcField" slot-scope="text, record, index"  >
                    <a-select
                              v-model="record.srcField" :options="fields" allowClear/>
                </template>

            </a-table>
        </a-form-model-item>
    </a-form-model>
</template>

<script>

import {GridMixin,Dialog} from "@lowcode/jpaas-common-lib";
import linkBase from "@/views/modules/form/core/cellRender/linkBase";
import FormBoPermissionApi from "@/api/form/core/formBoPermission";
/**
 * //列表ID
 * boListId: "",
 * //列表名称
 * name: "",
 * //别名
 * alias: "",
 * //{text:"",value:""}
 * boList: "",
 * //字段映射
 * fieldMap: [{srcField:"",tablePre:"",dataType:"",destField:"",fieldOp:""}]
 */


export default {
    name: "link-list-component",
    props:["layerid","destroy","config","fields"],
    mixins:[GridMixin,linkBase],
    data(){
        return {
            labelCol: {span: 6},
            wrapperCol: {span: 18},

            form: {
                name: "",
                alias: "",
                boList: "",
                pmtAlias: "",
                fieldMap: [],
            },
            dataAuthAry:[],
            selectedRowKeys: [],
            selectedRows: [],
            columns: [
                {
                    title: this.i18n('form.targetField','目标字段'),
                    dataIndex: 'destField',
                    scopedSlots: {customRender: 'destField'},
                },
                {
                    title: this.i18n('form.listField','当前列表字段'),
                    dataIndex: 'srcField',
                    scopedSlots: {customRender: 'srcField'}
                }
            ],
            rules: {
                boList: [
                    {required: true, message: this.i18n('msa.listNeed','列表必填'), trigger: 'blur'},
                ],
            }
        }
    },
    created(){
        this.init()
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"formCellRender",key);
        },
        init(){
            if(this.config){
                let obj=this.parse(this.config);
                if(obj.hasOwnProperty("alias")){
                    this.form=obj;
                    this.handDataAuths(this.form.alias);
                }
            }
        },
        parse(setting){
            if(typeof setting=="string"){
                return JSON.parse(setting);
            }
            return setting;
        },
        getData(){
            return this.form;
        },
        handSearchJson(searchJson){
            var ary=JSON.parse(searchJson);
            var rtn=ary.filter(item=>{
                return item.type=='income';
            }).map(item=>{
                //[{srcField:"",tablePre:"",dataType:"",destField:"",fieldOp:""}]
                return {
                    destField:item.fieldName,
                    label:item.fieldLabel,
                    fieldOp:item.fieldOp,
                    tablePre:item.tablePre,
                    dataType:item.dataType,
                    //源字段
                    srcField:""
                }});
            return rtn;
        },
        selectBoList(){
            var conf= {
                curVm: this,
                data: {single: true},
                widthHeight: ['1000px', '600px']
            };

            var self_=this;

            Dialog.openFormBoListDialog(conf,  (vm,data) => {
                var obj=data[0];
                var jsonObj={text:obj.name,value:obj.key};
                var json=JSON.stringify(jsonObj);
                self_.form.boList=json;
                self_.form.name=obj.name;
                self_.form.alias=obj.key;
                let list=self_.handSearchJson(obj.searchJson);
                self_.form.fieldMap=list;
                self_.handDataAuths(obj.key);
            })
        },
        handDataAuths(key){
            var self=this;
            FormBoPermissionApi.getListByKey(key).then(res=>{
                let ary=res.data.list || [];
                self.dataAuthAry=ary.map(item=>{
                    return {label:item.name,value:item.permissionId};
                })
            })
        },
        valid(){
            return new Promise((resolve,reject)=>{
                this.$refs.form.validate(valid => {
                    if(valid){
                        resolve(true)
                    }
                    else{
                        reject(false);
                    }
                });
            })
        },

    }
}
</script>

<style scoped>

</style>